* This file generates the results from Table 3, columns (3) and (4). The code for the first two columns are in "restat_psid_code.do"*

clear
set mem 2g

use psid-geocode
keep YEAR FAMID MSA00
ren FAMID in68
keep if YEAR==1968
drop YEAR
sort in68
ren MSA00 msa_orig
dmerge in68 using psid-fertility
ren _merge in68sample
tab in68sample
drop if in68sample==1
replace in68sample=1 if in68sample==3
replace in68sample=0 if in68sample==2
replace msa_orig=. if msa_orig>9900

gen temp="-"
egen temp2=concat(in68 temp pn68)
replace uniqueid=temp2 if year<2007
drop temp temp2
egen uniqueid2=group(uniqueid)

egen flag=tag(uniqueid2 year)
keep if flag==1

gen temp=msa if age==20
egen temp2=max(msa), by(uniqueid)
replace msa_orig=temp2 if msa_orig==.
replace msa_orig=. if msa_orig>9900
drop temp temp2

gen temp=msa if age==19
egen temp2=max(msa), by(uniqueid)
replace temp2=. if temp2>9900
replace msa_orig=temp2 if msa_orig==.
drop temp temp2

drop cmhpi1975-cmhpi2007
save psid-fertility-msaorig, replace

use msa, replace
ren msafp msa_orig
drop state pop1990 pop2000 pop2005
sort msa_orig
dmerge msa_orig using psid-fertility-msaorig
tab _merge
drop if _merge==1
drop _merge


gen pctcmhpidiff4_orig=cmhpi1985/cmhpi1981 if year==1985
replace pctcmhpidiff4_orig=cmhpi1986/cmhpi1982 if year==1986
replace pctcmhpidiff4_orig=cmhpi1987/cmhpi1983 if year==1987
replace pctcmhpidiff4_orig=cmhpi1988/cmhpi1984 if year==1988
replace pctcmhpidiff4_orig=cmhpi1989/cmhpi1985 if year==1989
replace pctcmhpidiff4_orig=cmhpi1990/cmhpi1986 if year==1990
replace pctcmhpidiff4_orig=cmhpi1991/cmhpi1987 if year==1991
replace pctcmhpidiff4_orig=cmhpi1992/cmhpi1988 if year==1992
replace pctcmhpidiff4_orig=cmhpi1993/cmhpi1989 if year==1993
replace pctcmhpidiff4_orig=cmhpi1994/cmhpi1990 if year==1994
replace pctcmhpidiff4_orig=cmhpi1995/cmhpi1991 if year==1995
replace pctcmhpidiff4_orig=cmhpi1996/cmhpi1992 if year==1996
replace pctcmhpidiff4_orig=cmhpi1997/cmhpi1993 if year==1997
replace pctcmhpidiff4_orig=cmhpi1999/cmhpi1995 if year==1999
replace pctcmhpidiff4_orig=cmhpi2001/cmhpi1997 if year==2001
replace pctcmhpidiff4_orig=cmhpi2003/cmhpi1999 if year==2003
replace pctcmhpidiff4_orig=cmhpi2005/cmhpi2001 if year==2005
replace pctcmhpidiff4_orig=cmhpi2007/cmhpi2003 if year==2007

gen pctcmhpidiff2_orig=cmhpi1985/cmhpi1983 if year==1985
replace pctcmhpidiff2_orig=cmhpi1986/cmhpi1984 if year==1986
replace pctcmhpidiff2_orig=cmhpi1987/cmhpi1985 if year==1987
replace pctcmhpidiff2_orig=cmhpi1988/cmhpi1986 if year==1988
replace pctcmhpidiff2_orig=cmhpi1989/cmhpi1987 if year==1989
replace pctcmhpidiff2_orig=cmhpi1990/cmhpi1988 if year==1990
replace pctcmhpidiff2_orig=cmhpi1991/cmhpi1988 if year==1991
replace pctcmhpidiff2_orig=cmhpi1992/cmhpi1990 if year==1992
replace pctcmhpidiff2_orig=cmhpi1993/cmhpi1991 if year==1993
replace pctcmhpidiff2_orig=cmhpi1994/cmhpi1992 if year==1994
replace pctcmhpidiff2_orig=cmhpi1995/cmhpi1993 if year==1995
replace pctcmhpidiff2_orig=cmhpi1996/cmhpi1994 if year==1996
replace pctcmhpidiff2_orig=cmhpi1997/cmhpi1995 if year==1997
replace pctcmhpidiff2_orig=cmhpi1999/cmhpi1997 if year==1999
replace pctcmhpidiff2_orig=cmhpi2001/cmhpi1999 if year==2001
replace pctcmhpidiff2_orig=cmhpi2003/cmhpi2001 if year==2003
replace pctcmhpidiff2_orig=cmhpi2005/cmhpi2003 if year==2005
replace pctcmhpidiff2_orig=cmhpi2007/cmhpi2005 if year==2007

gen cmhpi_orig=cmhpi1985 if year==1985
forval x=1986(1)2007{
replace cmhpi_orig=cmhpi`x' if year==`x'
}

gen predicted_pdiff4_orig=rhvaldiff_4*pctcmhpidiff4_orig
gen predicted_pdiff2_orig=rhvaldiff_2*pctcmhpidiff2_orig

save psid-fertility-msaorig, replace

use birthrate, clear
gen stfips=1 if state=="AL"
replace stfips=2 if state=="AK"
replace stfips=4 if state=="AZ"
replace stfips=5 if state=="AR"
replace stfips=6 if state=="CA"
replace stfips=8 if state=="CO"
replace stfips=9 if state=="CT"
replace stfips=10 if state=="DE"
replace stfips=11 if state=="DC"
replace stfips=12 if state=="FL"
replace stfips=13 if state=="GA"
replace stfips=15 if state=="HI"
replace stfips=16 if state=="ID"
replace stfips=17 if state=="IL"
replace stfips=18 if state=="IN"
replace stfips=19 if state=="IA"
replace stfips=20 if state=="KS"
replace stfips=21 if state=="KY"
replace stfips=22 if state=="LA"
replace stfips=23 if state=="ME"
replace stfips=24 if state=="MD"
replace stfips=25 if state=="MA"
replace stfips=26 if state=="MI"
replace stfips=27 if state=="MN"
replace stfips=28 if state=="MS"
replace stfips=29 if state=="MO"
replace stfips=30 if state=="MT"
replace stfips=31 if state=="NE"
replace stfips=32 if state=="NV"
replace stfips=33 if state=="NH"
replace stfips=34 if state=="NJ"
replace stfips=35 if state=="NM"
replace stfips=36 if state=="NY"
replace stfips=37 if state=="NC"
replace stfips=38 if state=="ND"
replace stfips=39 if state=="OH"
replace stfips=40 if state=="OK"
replace stfips=41 if state=="OR"
replace stfips=42 if state=="PA"
replace stfips=44 if state=="RI"
replace stfips=45 if state=="SC"
replace stfips=46 if state=="SD"
replace stfips=47 if state=="TN"
replace stfips=48 if state=="TX"
replace stfips=49 if state=="UT"
replace stfips=50 if state=="VT"
replace stfips=51 if state=="VA"
replace stfips=53 if state=="WA"
replace stfips=54 if state=="WV"
replace stfips=55 if state=="WI"
replace stfips=56 if state=="WY"

gen rincpercap=rpcincome*cpi
collapse (mean) rincpercap unemprate, by(stfips year)
sort stfips year
dmerge stfips year using psid-fertility-msaorig
tab _merge
drop if _merge~=3
drop _merge
gen lrincpercap=ln(rincpercap)

* Trim Sample *

replace asample=0 if tot_fam_y_lag0==9999999 & tot_fam_y_lag0~=.
replace asample=0 if tot_fam_y_lag0<-100000 & tot_fam_y_lag0~=.
replace asample=0 if (rhvaldiff_2<-30 | rhvaldiff_2>50) & rhvaldiff_2~=.
replace asample=0 if (rhvaldiff_4<-30 | rhvaldiff_4>150) & rhvaldiff_4~=.
replace asample=0 if (rhvaldiff_2<-30 | rhvaldiff_2>50) & rhvaldiff_2~=.
replace asample=0 if (housingpricemsadiff_4<-30 | housingpricemsadiff_4>150) & housingpricemsadiff_4~=. & ownhome==0
replace asample=0 if (housingpricemsadiff_2<-30 | housingpricemsadiff_2>50) & housingpricemsadiff_2~=. & ownhome==0

xtset uniqueid2 year
gen birth_1 = l1.birth
gen msa_4=l4.msa
gen mover=1 if msa_4~=msa
replace mover=. if msa_4==.

* Table 3 Original MSA Rregressions (columns 3 and 4) *

xi: reg birth predicted_pdiff2_orig married rtot_fam_y_lag0 fuchildren_lag0 age2024 age2529 age3034 age3539 age4044 hsdip somecoll ba edmiss unemp lrincpercap i.year i.msa if asample==1 & ownhome==1 & cmhpi2000~=. & age>24 & mover~=1, cluster(msa)
xi: reg birth predicted_pdiff4_orig married rtot_fam_y_lag0 fuchildren_lag0 age2024 age2529 age3034 age3539 age4044 hsdip somecoll ba edmiss unemp lrincpercap i.year i.msa if asample==1 & ownhome==1 & cmhpi2000~=. & age>24 & mover~=1, cluster(msa)
